//
//  SheZhiViewController.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "SheZhiViewController.h"

#import "GuanYuViewController.h"
#import "ZhiZuoRenViewController.h"


@interface SheZhiViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;


@end

@implementation SheZhiViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"关于", @"制作人", @"清空缓存", nil];
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"设置";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width, 20)];
    [self.tableView addSubview:label];
//    label.backgroundColor = [UIColor orangeColor];
    label.text = @"设置";
    label.alpha = 0.3;
    [label release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
    }
    cell.textLabel.text = self.arr[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:20];
    
    return cell;
}

// 点击方法,跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        GuanYuViewController *guanyu = [[GuanYuViewController alloc] init];
        [self.navigationController pushViewController:guanyu animated:YES];
    } else if (indexPath.row == 1){
        ZhiZuoRenViewController *zhizuoren = [[ZhiZuoRenViewController alloc] init];
        [self.navigationController pushViewController:zhizuoren animated:YES];
    } if (indexPath.row == 2) {
        
#pragma mark 清除缓存
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否清除缓存" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        
        
        [alert show];
        [alert release];
        
        
        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        
        // 清除缓存
        // 沙盒路径
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        // 创建一个文件管理者
        NSFileManager *manager = [NSFileManager defaultManager];
        
        if (sandBox == nil) {
            NSLog(@"创建路径");
        
        // 通过文件管理者进行文件夹的创建
        // 创建一个文件夹对应的路径
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Movie.plist"];
        [manager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
        } else {
//            UIAlertView *alertview1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缓存已清理,无需再清" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alertview1 show];
//            [alertview1 release];
            
        
        
        NSArray *cachesArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES);
        NSString *cachesPath = cachesArr[0];
        NSLog(@"%@", cachesPath);
        [manager removeItemAtPath:cachesPath error:nil];
        NSLog(@"已清除缓存");
        }
    } else {
        NSLog(@"取消");
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
